/*
 * @Author: zhangjingyu
 * @Date: 2020-12-02 12:22:16
 * @LastEditors: zhangjingyu
 * @LastEditTime: 2021-05-18 17:02:43
 * @FilePath: \vprisk10.14\src\main.js
 * @Description:入口文件
 */
// js兼容性
import 'core-js/stable';
import 'regenerator-runtime/runtime';

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 变色龙项目需要的依赖
import Avue from '@smallwei/avue'
import '@smallwei/avue/lib/index.css'
import avueFormDesign from 'pig-avue-form-design'
Vue.use(Avue, {
  size: 'small',
  menuType: 'text'
})
Vue.use(avueFormDesign)
// 先引入重置样式，后面文件可覆盖
import axios from '@/api/config'
window.axios = axios
// 全局配置
import '@/assets/scss/reset.scss'
import 'element-ui/lib/theme-chalk/index.css'
import HTTP from '@/api/config'
//import './mock'
import glo_loading from '@/config/glo_loading' //loading组件的简单封装
import {routerLoading} from '@/config/loadingConfig' //全局的页面跳转loading是否启用
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI, {
  size: 'small',
  menuType: 'text'
})
// 第三方包
import './lib/element'



//
// 自定义组件 
import Contextmenu from 'vue-contextmenujs'
import ForMenu from './components/ForMenu.vue'
import HoverMenu from './components/HoverMenu.vue'
import pagination from './components/pagination.vue'
import onlyOneSearch from "./components/onlyOneSearch.vue"
import upperTemplate from "./components/upperTemplate.vue"
import vpTree from "./components/vpTree.vue"
Vue.use(Contextmenu);
Vue.component('ForMenu', ForMenu);
Vue.component('HoverMenu', HoverMenu);//展开三级时右侧悬浮菜单
Vue.component('pagination', pagination);// 分页组件
Vue.component('upperTemplate', upperTemplate);// 按钮 icon 操作区模板
Vue.component('onlyOneSearch', onlyOneSearch);// 单个悬浮搜索
Vue.component('vpTree', vpTree);
// 自定义方法
import {getLabel,tableDataPick,listToTree,deepClone} from '@/lib/util'
Vue.prototype.$getLabel = getLabel // list翻译方法
Vue.prototype.$tableDataPick = tableDataPick // table列隐显控制方法
Vue.prototype.$listToTree = listToTree // list转tree方法
Vue.prototype.$clone = deepClone // 深拷贝方法
import { getDict } from "@/api/getDict";
Vue.prototype.$getDict = getDict // 方便获取字典
// 
Vue.prototype.bus = new Vue() // 组件之间的传值
import Vue2OrgTree from 'vue-tree-color'
Vue.use(Vue2OrgTree)
// 窗口拖拽js
import './lib/drag'
// 样式 
import '@/assets/scss/index.scss'
// 兼容性样式覆盖
import './lib/cssAuto'
// 以下为bpmn工作流绘图工具的样式
import 'bpmn-js/dist/assets/diagram-js.css' // 左边工具栏以及编辑节点的样式
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css'
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-codes.css'
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css'
// 将http对象挂载到Vue原型上，实现所有Vue实例都可以通过$http调用这个HTTP对象
Vue.prototype.$http = HTTP
Vue.prototype._GLOBAL_ = 6e3
Vue.config.productionTip = false

import basicContainer from './components/basic-container/main'

// 注册全局容器
Vue.component('basicContainer', basicContainer)


// 添加路由守卫拦截，判断用户登录状态以跳转到对应页面
router.beforeEach((to, from, next) => {
  routerLoading&& to.name != 'login' ? glo_loading.loadingShow() : '' //如果全局启用页面跳转则加载loading,登陆页不显示loading
    let token = sessionStorage.getItem('loginToken') || null
  if (!token && to.name !== 'login') {
    next({ name: 'login' })
  }
  // 除报表外，如果匹配不到路由，则显示404页面(报表路由可配置reportForm=true,后续开发完善)
  else if ((!to.name || to.matched.length == 0) && !to.reportForm) {
    next({name:'404'})
  } else {
    next()
  }
  setTimeout(() => {
    routerLoading ? glo_loading.loadingHide() : ''//关闭loading层
  },0)
})

   // 处理路由懒加载会偶发出现Loading chunk
router.onError((error) => {
     console.log(error)
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);
    console.log(isChunkLoadFailed,'/Loading chunk (\d)+ failed/g','路由懒加载找不到对应的moudle')
    if (isChunkLoadFailed) { 
        window.location.reload();
      }else{
        console.log(error)
    }
   });
  
new Vue({
  router,
  store,
  render: h => h(App),
  created() {
    store.commit('addMenu', router)
  }
}).$mount('#app')
